import { Observable } from "./observable";
import '@babel/polyfill'; //编译async的包 
import { UploadManager } from "./upload";
import { TransManager } from "./trans";

function upload(bucketName, file, objectName, storage) {

  let options = {
    file,
    bucketName,
    objectName,
    storage
  };

  return new Observable(observer => {
    let uploadManager = new UploadManager(options, {
      onData: e => observer.next(e),
      onError: e => observer.error(e),
      onComplete: e => observer.complete(e)
    });
    uploadManager.putFile();
    return uploadManager.stop.bind(uploadManager);
  });
}

function trans(bucketName, objectName, storage, transOptions) {

  let options = {
    bucketName,
    objectName,
    storage,
    transOptions
  };

  return new Observable(observer => {
    let transManager = new TransManager(options, {
      onData: e => observer.next(e),
      onError: e => observer.error(e),
      onComplete: e => observer.complete(e)
    });
    transManager.trans();
    return transManager.stop.bind(transManager);
  });
}

export {
  upload,
  trans
}